// pages/appointmentOrder/appointmentOrder.js
const app = getApp()
const $api = require('../../api/index')
Page({
    /**
     * 页面的初始数据
     */
    data: {
        navTop: app.globalData.navTop,
        appointmentTime: '请选择时长', //预约时间
        extendTime: '请选择时长', //延长预约时间
        show: false,
        currentDate: '',
        minHour: 0,
        maxHour: 24,
        overlayShow: false, //遮罩层开启或关闭
        appointmentStatus: 0, //0成功,1失败
        overlayText: ['请在预约时间结束前连接充电\n过时将自动视为终止预约\n 并收取等额服务费', '当前预约人数过多\n请稍后再试'],
        overlayImage: ['../../static/images/appointmentOrder-success.png', '../../static/images/appointmentOrder-fail.png'],
        orderId: '',
        price: '', //预约服务费
        surplusTime: '' //剩余时间
    },
    setAppointmentTime(type) { //时间选择弹出框
        this.setData({
            show: true
        })
    },
    goBack() { //返回
        wx.navigateBack()
    },
    onClose() { //关闭遮罩
        this.setData({
            show: false
        })
    },
    placeAnOrder() { //立即下单
        if (!this.data.currentDate) {
            wx.showToast({
                title: '请选择预约时间或延长时间',
                icon: 'none',
                duration: 3000,
            })
            return false
        }
        let time = Number(this.data.currentDate.split(":")[0]) * 60 + Number(this.data.currentDate.split(":")[1])
        if (this.data.orderId) { //存在订单号,延时
            let data = {
                trade_no: this.data.orderId,
                subscribe_time: time
            }
            $api.subscribeDelay(data).then((res) => {
                this.setOverlayShow(res)
            })
        } else { //不存在订单号,非延时
            let data = {
                terminal_id: this.data.id,
                subscribe_time: time
            }
            $api.subscribePlaceOrder(data).then((res) => {
                this.setOverlayShow(res)
            })
        }

    },
    setOverlayShow(data) { //预约弹窗
        this.setData({
            overlayShow: true,
            appointmentStatus: data.data.status == 1 ? 0 : 1,
            orderId: data.data.trade_no
        })
        if (data.data.status === 0) {
            this.setData({
                'overlayText[1]': data.message
            })
        }
    },
    onInput({
        detail
    }) { //时间选择器确认
        let time = detail.split(":")[0] + "时" + detail.split(":")[1] + '分'
        if (this.data.orderId) {
            this.setData({
                extendTime: time,
            })
        } else {
            this.setData({
                appointmentTime: time,
            })
        }
        this.setData({
            currentDate: detail,
            show: false
        })
    },
    onClickHide() { //遮罩层关闭
        this.setData({
            overlayShow: false
        })
        if (this.data.appointmentStatus === 0) { //如果是成功状态则跳转到倒计时并且关闭当前页面
            wx.navigateTo({
                url: '../appointmentSuccessful/appointmentSuccessful?orderId=' + this.data.orderId
            })
        }
    },
    getList(id) { //预约预览
        let data = {
            terminal_id: id,
        }
        $api.subscribePreview(data).then((res) => {
            this.setData({
                price: res.data.price,
                parking_no:res.data.parking_no
            })
        })
    },
    getsubscribeDetail(orderId) { //根据订单号获取预约时间
        let data = {
            trade_no: orderId,
        }
        $api.subscribeDetail(data).then((res) => {
            this.setData({
                surplusTime: res.data.surplus_time,
                price: res.data.service_price,
                parking_no: res.data.parking_no
            })
        })

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            orderId: options.orderId,
            id: options.id
        })
        if (options.orderId) {
            this.getsubscribeDetail(options.orderId)
        } else {
            this.getList(options.id)
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})